#!/bin/bash
## 
## Ubuntu Cleaner version 0.3.3
## Copyright 2008 Petri Järvisalo [petri.jarvisalo@mbnet.fi]
## MasseR [mats.rauhala@gmail.com]
## 
##     This file is part of ubuntuclean.
##
##    ubuntuclean is free software: you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation, either version 3 of the License, or
##    (at your option) any later version.
##
##    ubuntuclean is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with ubuntuclean.  If not, see <http://www.gnu.org/licenses/>.


#Dialog sample
#dialog_temp="/tmp/dialog.tmp"
# dialog --checklist "Choose actions:" 10 40 3 \
#        -a All off \
#	-f "Firefox" off \
#	-t Trash off 2> $dialog_temp
# cat $dialog_temp


TEXTDOMAINDIR=/usr/local/share/locale
TEXTDOMAIN=cleanubuntu
dialog_temp="/tmp/dialog.tmp"

export TEXTDOMAINDIR
export TEXTDOMAIN

version="0.3.3";

. gettext.sh

E_CDERROR=65

error()
{
  printf "$@" >&2
  exit $E_CDERROR
}

init() {
echo -e "\n\033[1;32m `gettext "UbuntuCleaner"` $version \033[1;34m \n"
echo -e "`gettext "Initializing..."`\033[0;37m\n"
if [ $(pgrep firefox|wc -l) -gt 0 ]; then              # check if any instances of firefox running
	firefoxrun=true
fi
if [  $(pgrep opera|wc -l) -gt 0 ]              # check if any instances of opera running
then
	operarun=true
fi

## Check if konqueror exists
if [ -d $HOME/.kde/share/apps/konqueror ]
then
	kaffeine_exists=true;
fi

## check if opera exists in system
if [ -d $HOME/.opera ] 
then 
Opera_exists=true; 
fi;

##kaffeine directory
targetKaffeineFile="$HOME/.kde/share/config/kaffeinerc"
## temp file for dialog

}

kde_clean() {
	#Keskeneräinen
	if [ -d $HOME/.kde/share/apps/RecentDocuments ]; then
		rm -fr $HOME/.kde/share/apps/RecentDocuments/*
	fi
}

konqueror_clean() {
	if [ $konqueror_exists==true ]
	then
		echo "cleaning konq"
		#Clean konqueror history
		konqhist=$HOME/.kde/share/apps/konqueror/konq_history
		if [ -e $konqhist ]
		then
			#rm $konqhist
			#touch $konqhist
			echo > $konqhist
		fi
		#Clean kde-cache
		#Emphasize: _KDE_ caches
		konqcache=$HOME/.kde/cache-$HOSTNAME/http
		if [ -d $konqcache ]
		then
			rm -fr $konqcache/*
		fi
		#Clean kde cookies
		#Emphasize: _KDE_ cookies
		konqcookies=$HOME/.kde/share/apps/kcookiejar/cookies
		if [ -e $konqcookies ]
		then
			#rm $konqcookies
			#touch $konqcookies
			echo > $konqcookies
		fi
	else
		#Bug, it had "Kaffeine"
		echo "$(gettext "Konqueror is not installed, thus not cleaned")"
	fi
}

bash_remove() { 
echo " `gettext "Deleting bash history.."`"
if [ -e "$HOME/.bash_history" ]; then rm "$HOME/.bash_history"; fi
echo -e "\033[1;32mOk\033[0;37m"
}

trash() {
echo " `gettext "Dumping trash.."`"
if [ -d $HOME/.local/share/Trash/ ] 
then
rm -r $HOME/.local/share/Trash/ 
fi
echo -e "\033[1;32mOk\033[0;37m"
}

flash() {
echo " `gettext "Clearing flash history.."`"
if [ -d $HOME/.macromedia ] 
then
rm -r $HOME/.macromedia
echo -e "\033[1;32mOk\033[0;37m"
else 
echo -e "\033[1;33m`gettext "Not found / already cleaned"`\033[0;37m"
fi
}

gnome() {
echo "`gettext "Clearing gnome thumbnails.."`"
if [ -e $HOME/.thumbnails ] 
then
rm -r $HOME/.thumbnails
echo -e "\033[1;32mOk\033[0;37m"
else 
echo -e "\033[1;33m`gettext "Not found / already cleaned"`\033[0;37m"
fi

echo "`gettext "Clearing recently used items on gnome.."`"
oldfile=$HOME/.recently-used
newfile=$HOME/.recently-used.xbel
#I tested this quite extensively, should work just fine :)
#wc -c is a helluva lot faster than wc -l
#echo > $oldfile && wc -c $oldfile will return 1, therefore -gt 1
if [[ -e $oldfile && $(wc -c $oldfile | awk '{print $1}') -gt 1 || -e $newfile && $(wc -c $newfile | awk '{print $1}') -gt 1 ]]
then
	# If it really is connected to gnome-panel, maybe by shutting it down _first_ then clearing the files, and then starting it again would help?
	killall -9 gnome-panel
	echo > $newfile
	echo > $oldfile
echo -e "\033[1;32mOk\033[0;37m"
else 
echo -e "\033[1;33m`gettext "already cleared."`\033[0;37m";
fi
}

firefox() {
#This check could be done more elegantly, but this was a quick fix. Might stay this way depending on the users
#The same goes for opera
if [ $firefoxrun==true ]
then
	echo -e "\033[1;31m `gettext "Firefox is running, please exit all instances and try again."` \033[0;37m\n"
	exit 1
fi
if [ -z $1 ] 
then
	profiles=$(awk '/Path/ {split($1,a,"="); printf("%s/.mozilla/firefox/%s\n",ENVIRON["HOME"],a[2])}' $HOME/.mozilla/firefox/profiles.ini)
else
	#Needs testing
	profiles=$(ls $HOME/.mozilla/firefox/ | grep $1 | xargs -i echo "$HOME/.mozilla/firefox/{}")
	if [ -z $profiles ]
	then
		exit 1
	fi
fi
echo "`gettext "Clearing firefox cache.."`"
echo $profiles
for i in $profiles
	
	do
	if [ -d $i/Cache ]
	then 
		cd $i/Cache
		if [ "`ls | wc -l`" -gt "0" ]
		then
			rm *
			echo -e "\033[1;32mOk\033[0;37m";
		else 
			echo -e "\033[1;33m`gettext "already cleared."`\033[0;37m";
		fi
	
	else
		echo -e "\033[1;31m`gettext "Fail"`\033[0;37m";	
		
		
	fi

	echo "`gettext "Clearing firefox offline cache.."`"
	if [ -d $i/OfflineCache ]
	then
	cd $i/OfflineCache
		if (( !$? )); 
		then 
		if [ "`ls | wc -l`" -gt "0" ]
		then
			rm *
			echo -e "\033[1;32mOk\033[0;37m";
		else 
			echo -e "\033[1;33m`gettext "already cleared."`\033[0;37m";
		fi
		fi

	else 
	echo -e "\033[1;31m`gettext "Fail, offline cache not found"`\033[0;37m";
	fi


	echo "`gettext "Clearing firefox cookies.."`"
	if [ -e $i/cookies.sqlite ]
	then
		rm $i/cookies.sqlite;
		touch $i/cookies.sqlite;
		echo -e "\033[1;32mOk\033[0;37m";
	else if [ -e $i/cookies.dat ]
		then
			rm $i/cookies.dat
			touch $i/cookies.dat
			echo -e "\033[1;32mOk\033[0;37m";
	else
		echo -e "\033[1;33m`gettext "Not found / already cleaned"`\033[0;37m"	
	fi


	echo "`gettext "Clearing firefox form history.."`"
	if [ -e $i/formhistory.sqlite ]
	then
	rm $i/formhistory.sqlite
	echo -e "\033[1;32mOk\033[0;37m"
	else
	echo -e "\033[1;33m`gettext "Not found / already cleaned"`\033[0;37m"
	fi




	echo "`gettext "Clearing firefox2 history.."`"
	if [ -e $i/history.dat ]
	then
		rm $i/history.dat
	echo -e "\033[1;32mOk\033[0;37m\n\n"
	else 
	echo -e "\033[1;33m`gettext "Not found / already cleaned"`\033[0;37m"
	fi

	echo "`gettext "Clearing zoom-history.."`"
	if [ -e $i/content-prefs.sqlite ]
	then
	rm $i/content-prefs.sqlite
	echo -e "\033[1;32mOk\033[0;37m\n\n"
	else 
	echo -e "\033[1;33m`gettext "Not found / already cleaned"`\033[0;37m"
	fi

	echo "`gettext "Clearing download-history.."`"
	if [ -e $i/downloads.sqlite  ]
	then
	rm $i/downloads.sqlite 
	echo -e "\033[1;32mOk\033[0;37m\n\n"
	else 
	echo -e "\033[1;33m`gettext "Not found / already cleaned"`\033[0;37m"
	fi
	
	echo "`gettext "Clearing firefox3 history.."`"
	if [ -e $i/places.sqlite ]
	then
	sqlite3 $i/places.sqlite "delete from moz_places where id not in (select fk from moz_bookmarks) and url not like 'places:%'"
	echo -e "\033[1;32mOk\033[0;37m\n\n"
	else
	echo -e "\033[1;33m`gettext "Not found / already cleaned"`\033[0;37m"
	fi
	
fi
done
} 

#### opera reset
opera() {
if [ $operarun==true ]
then
	echo -e "\033[1;31m `gettext "Opera is running, please exit all instances and try again."` \033[0;37m\n"
	exit 1
fi
if [ $Opera_exists == "true" ] 
then
	##Opera cookies
	echo "`gettext "Clearing opera cookies.."`"
	if [ -e $HOME/.opera/cookies4.dat ]
	then 
	rm $HOME/.opera/cookies4.dat
	echo -e "\033[1;32mOk\033[0;37m"
	else
	echo -e "\033[1;33m`gettext "Not found / already cleaned"`\033[0;37m"
	fi
	##Opera recent downloads
	echo "`gettext "Clearing opera recent downloads.."`"
	if [ -e $HOME/.opera/download.dat ]
	then 
	rm $HOME/.opera/download.dat
	echo -e "\033[1;32mOk\033[0;37m"
	else
	echo -e "\033[1;33m`gettext "Not found / already cleaned"`\033[0;37m"
	fi
	##Opera visited sites
	echo "`gettext "Clearing opera recent downloads.."`"
	if [ -e $HOME/.opera/vlink4.dat ]
	then 
	rm $HOME/.opera/vlink4.dat
	echo -e "\033[1;32mOk\033[0;37m"
	else
	echo -e "\033[1;33m`gettext "Not found / already cleaned"`\033[0;37m"
	fi
	##Opera cache
	echo "`gettext "Clearing opera cache.."`"
	if [ -d $HOME/.opera/cache4 ]
	then 
	rm -r $HOME/.opera/cache4
	echo -e "\033[1;32mOk\033[0;37m"
	else
	echo -e "\033[1;33m`gettext "Not found / already cleaned"`\033[0;37m"
	fi
	##opera opcache
	echo "`gettext "Clearing more opera cache.."`"
	if [ -d $HOME/.opera/opcache ]
	then 
	rm -r $HOME/.opera/opcache
	echo -e "\033[1;32mOk\033[0;37m"
	else
	echo -e "\033[1;33m`gettext "Not found / already cleaned"`\033[0;37m"
	fi
	
else 
echo "`gettext "Browser Opera not detected. Skipping."`" 
fi
}

#### Kaffeine reset
kaffeine() {
echo "`gettext "Clearing kaffeine recent documents.."`"
hKFind1="^\[Recent Files\]"
hKFind2="^$"

if [ -e "$targetKaffeineFile" ]
then
sed -i '/'"$hKFind1"'/,/'"$hKFind2"'/d' "$targetKaffeineFile"
echo -e "\033[1;32mOk\033[0;37m\n\n"
else
echo -e "\033[1;33m`gettext "Not found"`\033[0;37m"	
fi
}
apt() {
echo -e "\n\033[1;32m `gettext "Removing obsolete packages:"` \033[1;34m \n"
if [ $USER != root ]; then
	echo -e "\033[1;31m`gettext "Fail: Requires root privileges"`\033[0;37m"
	echo -e "\033[1;33m`gettext "Use 'sudo cleanubuntu' with --apt option"`\033[0;37m"
	exit 126 # exit with "command invoked cannot execute" ie permission problem.
fi
apt-get -y autoremove 
	if [ $? -gt "0" ]  # This solution appears to work
	then
	echo -e "\033[1;31m`gettext "Fail, something went wrong"`\033[0;37m";
	exit 1
	else
apt-get -y clean
	if [ $? -gt "0" ] 
		then
		echo -e "\033[1;31m`gettext "Fail, something went wrong"`\033[0;37m";
		exit 1
		else 
		echo -e "\033[1;32mOk\033[0;37m\n\n"
		fi
	fi
}

interact () {
if [ ! -e /usr/bin/dialog ] 
then 
echo -e "\n this feature requires dialog, please install it by typing:"
echo -e "sudo apt-get install dialog"
echo -e "and try again."
exit 1
else
dialog --checklist "`gettext "Select options:"`" 20 50 9 \
        -A "`gettext "Clean All"`" off \
	-a "`gettext "Clean apt-cache"`" off \
	-b "`gettext "Clean bash-related"`" off \
	-f "`gettext "Clean firefox (single profile)"`" off \
	-F "`gettext "Clean firefox (all profiles)"`" off \
	-g "`gettext "Clean GNOME-history"`" off \
	-o "`gettext "Clean Opera"`" off \
	-k "`gettext "Clean Kaffeine"`" off \
	-t "`gettext "Clean Trashbin"`" off 2> $dialog_temp
cleanubuntu_switches=`sed 's/"//g' $dialog_temp`
	if [ "$cleanubuntu_switches" == "" ]
	then
	clear	
	echo "`gettext "Nothing selected. Nothing to clean."`"
	else
	cleanubuntu $cleanubuntu_switches
	fi

exit 0
fi
}

uchelp() {
	#Prints the --help
	echo "`gettext " -A|--all				Clean all"`";
	echo "`gettext " -a|--apt				Clean apt-cache"`";
	echo "`gettext " -b|--bash				Clean bash-related"`";
	echo "`gettext " -f|--firefox-single-profile		Clean firefox-data with single profile"`";
	echo "`gettext " -F|--firefox-all-profiles		Clean firefox-data with all profiles"`";
	echo "`gettext " -o|--opera				Clean Opera data"`";
	echo "`gettext " -K|--konqueror				Clean konqueror data (Not tested!)"`";
	echo "`gettext " -g|--gnome-history			Clean bash history"`";
	echo "`gettext " -h|--help				Show this help text"`";
	echo "`gettext " -i|--interactive			Choose options with gui"`";
	echo "`gettext " -k|--kaffeine				Clean Kaffeine history"`";
	echo "`gettext " -t|--trash				Empty trash"`";
	exit 0 # we don't wanna see all done-text after this.
	
}

switchcontrol ()

{
	if [ -z $1 ]
	then
		echo "`gettext "try --help"`"
		exit 0 # we don't wanna see all done-text after this.
	else
		while [ $# -gt 0 ]; do
			case "$1" in
			  -f|--firefox-single-profile)
			  	shift 
				if [ -z "$1" ]
				then
			  		echo "`gettext "Error: No profile given"`" 
					exit 1
				else
					case "$1" in
						[-]*)  
			  				echo "`gettext "Error: No profile given"`" 
							exit 1
							;;
						*) 
						firefox $1
						;;
					esac
				fi
				#Check if a profile is given
			  	;;
			  -F|--firefox-all-profiles)
				init
			  	firefox
			  	;;
			  -o|--opera)
				init
			  	opera
			  	;;
			  -K|--konqueror)
				init
			  	konqueror_clean
			  	;;
			  -b|--bash)
				init
			  	bash_remove
			  	;;
			  -t|--trash)
				init
			  	trash
			  	;;
			  -g|--gnome-history)
				init
			  	gnome
			  	;;
			  -k|--kaffeine)
				init
			  	kaffeine
			  	;;
			  -a|--apt)
				init
			  	apt
			  	;;
			  -A|--all)
				init
			  	bash_remove
				firefox
				trash
				gnome
				opera
				kaffeine
			  	;;
			  -h|--help)
			  	uchelp
			  	;;
			  -i|--interactive)
				interact
			esac
			shift
		done
	fi
}

switchcontrol $*
echo -e "`gettext "All done!"`\n\033[0;37m"
